package org.example.dao.impl;

import org.example.dao.OrderDao;
import org.example.entity.Order;

import java.util.List;

public class OrderDaoimpl implements OrderDao {
    @Override
    public void addOrder(Order order) {
        String sql = "INSERT INTO db_ord VALUES(NULL,?,?,?,?,?)";
        int row = DBUtil.executeSql(sql,order.getUserId(),order.getTotalAmount(),order.getOrderStatus(),order.getCreateTime(),order.getPayTime());
        if(row > 0) {
            System.out.println("添加成功");
        }
    }

    @Override
    public void delOrder(int orderId) {
        String sql = "DELETE FROM db_ord WHERE ord_id = ?";
        int row = DBUtil.executeSql(sql,orderId);
        if(row > 0) {
            System.out.println("删除成功");
        }
    }

    @Override
    public void updOrder(Order order) {
        String sql = "UPDATE db_ord SET user_id = ?,total_amount = ?,ord_status = ?,create_time = ?, pay_time = ? WHERE ord_id = ?";
        int row = DBUtil.executeSql(sql, order.getUserId(), order.getTotalAmount(), order.getOrderStatus(), order.getCreateTime(), order.getPayTime(), order.getOrderId());
        if (row > 0) {
            System.out.println("更新成功");
        }
    }

    @Override
    public Order getOrderById(int id) {
        String sql = "select * from db_ord where ord_id = ?";
        return DBUtil.executeQuery(Order::map,sql,id).get(0);
    }

    @Override
    public List<Order> getAllOrders() {
        String sql = "select * from db_ord";
        return DBUtil.executeQuery(Order::map,sql);
    }
}